package com.kye.express.business.tcl.home;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.tcl.RequestTCLGoodsList;
import com.kye.model.bean.tcl.ResponseTCLGoodsListBean;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.TCLDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * TCL首页 Presenter
 * <p>
 * Created by SkyYu on 2018/2/1
 */
public interface TCLContract {

    interface TCLView extends IView {
        void onLoadUser(User user);

        void onCompleteTCLGoodsList(boolean isLoadMore);

        void onSuccessTCLGoodsList(OpenApiResult<ResponseTCLGoodsListBean> bean, boolean isFirstIn, boolean isReFresh, boolean isLoadMore);

        void onApiErrorTCLGoodsList(OpenApiResult<ResponseTCLGoodsListBean> result, int code, String msg);

        void onOtherErrorTCLGoodsList(Throwable e);
    }

    class TCLPresenter extends BasePresenter<TCLView> {

        TCLDataSource mTclDataSource;

        TCLPresenter() {
            mTclDataSource = new TCLDataSource();
        }

        @Override
        public void bind(TCLContract.TCLView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(user ->
                            getView().onLoadUser(user)
                    );
        }

        void requestToJDGoodsList(boolean isFirstIn, boolean isReFresh, boolean isLoadMore, String phone, int pageIndex, int pageSize) {
            mTclDataSource.getTCLGoodsList(new RequestTCLGoodsList(phone,pageIndex,pageSize))
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResponseTCLGoodsListBean>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {
                            getView().onCompleteTCLGoodsList(isLoadMore);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResponseTCLGoodsListBean> result, ResponseTCLGoodsListBean jdGoodsListResultBean) {
                            getView().onSuccessTCLGoodsList(result, isFirstIn, isReFresh, isLoadMore);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResponseTCLGoodsListBean> result, int code, String msg) {
                            getView().onApiErrorTCLGoodsList(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherErrorTCLGoodsList(e);
                        }
                    });
        }
    }
}